<?php
namespace Home\Controller;
use Think\Controller;
class RegController extends BaseController {

	private $m = NULL;
	private $navName = '微信二维码用户注册';
	
	public function _initialize(){
		parent::_initialize();
		$this->md = D('Api/Member');
		$this->mx = M('member_info');
		$this->userid = I('uid');
		$this->sign = I('sign');
		
			
	}
   
       public function wx_info(){
           $this->writelog_name(var_export($_REQUEST,TRUE),"1"); 

		$appid = "wx26159985ac0ff818"; 
		//wx26159985ac0ff818
		$secret = "35efe006c39cc7c79e345542aed1954f"; 
		$code = $_GET["code"]; 
		$get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code';
               
				$ch = curl_init();
				curl_setopt($ch,CURLOPT_URL,$get_token_url);
				curl_setopt($ch,CURLOPT_HEADER,0);
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
				curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
				$res = curl_exec($ch);
				curl_close($ch); 
	
		$json_obj = json_decode($res,true); 
		//根据openid和access_token查询用户信息 
		$access_token = $json_obj['access_token']; 
		$openid = $json_obj['openid'];
           
               // $get_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'lang=zh_CN';
                $get_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN';
                $fo = curl_init();/// 初始化一个 cURL 对象
		curl_setopt($fo,CURLOPT_URL,$get_info_url); // 设置你需要抓取的URL
		curl_setopt($fo,CURLOPT_HEADER,0); // 设置header
		curl_setopt($fo, CURLOPT_RETURNTRANSFER, 1 );  // 设置cURL 参数，要求结果保存到字符串中还是输出到屏幕上。
                //curl_setopt($fo,CURLOPT_POST,1);
		curl_setopt($fo, CURLOPT_CONNECTTIMEOUT, 10); //设置连接等待时间
		$resfo = curl_exec($fo); //函数的作用是执行一个curl会话，唯一的参数是curl_init()函数返回的句柄。
		curl_close($fo); //关闭一个curl会话
                $json_info = json_decode($resfo,true); 
                           
                if($json_info['nickname'] == ''){
                   redirect('http://www.judaren.com.cn/jdrwechat/author.php?vid='.$_GET['vid']);   
                }
				
                $data['sex'] = $json_info['sex'];
                $data['nickname'] = $json_info['nickname'];
                $data['headimgurl'] = $json_info['headimgurl'];
                $data['city'] = $json_info['city'];
                $data['openid'] = $json_info['openid'];
                $data['vid'] = $_GET['vid']?$this->decode($_GET['vid']):0;
                $data['pid'] = $_GET['pid']?$_GET['pid']:0;
               /*验证微信是否已被扫描注册接口   返回状态说明   0 微信号未注册    1微信号已被注册     2 openid为空
                * 09/13
                * liuq
                */
                $get_info_openid = 'http://www.judaren.com.cn/jdrapi/Api.php/Suclogin/reg_wx_openid?openid='.$openid.'&lang=zh_CN';
                $io = curl_init();/// 初始化一个 cURL 对象
		curl_setopt($io,CURLOPT_URL,$get_info_openid); // 设置你需要抓取的URL
		curl_setopt($io,CURLOPT_HEADER,0); // 设置header
		curl_setopt($io, CURLOPT_RETURNTRANSFER, 1 );  // 设置cURL 参数，要求结果保存到字符串中还是输出到屏幕上。
               // curl_setopt($io,CURLOPT_POST,1);
		curl_setopt($io, CURLOPT_CONNECTTIMEOUT, 10); //设置连接等待时间
		$resio = curl_exec($io); //函数的作用是执行一个curl会话，唯一的参数是curl_init()函数返回的句柄。
		curl_close($io); //关闭一个curl会话
                $json_inio = json_decode($resio,true); 
              
                if(!empty($json_inio) && $json_inio == 1){
                    $this->display('Reg/regc');exit();
                }
                
                
                
                
                $jssdk = new \Org\weixin\Jssdk('wx26159985ac0ff818', '35efe006c39cc7c79e345542aed1954f');
                $signPackage = $jssdk->GetSignPackage();
            //   print_r($signPackage);exit();
                $this->assign('signPackage',$signPackage);
				 $this->writelog_name(var_export($data,TRUE),"2"); 
                $this->assign('data',$data);
                $this->display('Reg/rega');

        }
        
        
        public function reg_first(){
                   
                $mobile = I('mobile');
               /*验证电话是否被注册过 返回状态说明   0 未注册    1 已被注册 
                * liuq
                */
                $get_mobile_url = 'http://www.judaren.com.cn/jdrapi/Api.php/Suclogin/reg_wx_mobile?mobile='.$mobile.'&lang=zh_CN';
                $mo = curl_init();
		curl_setopt($mo,CURLOPT_URL,$get_mobile_url);
		curl_setopt($mo,CURLOPT_HEADER,0); 
		curl_setopt($mo, CURLOPT_RETURNTRANSFER, 1 );  
                //curl_setopt($fo,CURLOPT_POST,1);
		curl_setopt($mo, CURLOPT_CONNECTTIMEOUT, 10); 
		$resmo = curl_exec($mo); 
		curl_close($mo); 
                $json_mobile = json_decode($resmo,true); 
             //  print_r($json_mobile);exit();
            
                 if($json_mobile == 1){
		 $data=100;
                 $this->ajaxReturn($data);
                 }
                 
                 
	        $code = I('code');
                if( $code == '' ){
                $data=300;
                $this->ajaxReturn($data);
		};      
                 
            
                if($code != $_SESSION['sms_mobile_code'] )
                    {
                $data=200;
                $this->ajaxReturn($data);
		};
                
                  
         
		 $data=400;
                $this->ajaxReturn($data);
                
		
           }
		
           

                public function reg_second(){
				
				if(empty(I('mobile'))){   echo"<script>alert('参数不能为空！');history.go(-1);</script>"; exit;   }
                if(empty(I('password'))){   echo"<script>alert('参数不能为空！');history.go(-1);</script>"; exit;   }
			    if(empty(I('openid'))){   echo"<script>alert('参数不能为空！');history.go(-1);</script>"; exit;   }
				
			  
				
                $post_data['mobile'] = I('mobile');
                $post_data['password'] = I('password');
                $post_data['nickname'] = I('nickname');
                $post_data['sex'] = I('sex');
                $post_data['headimgurl'] = I('headimgurl');
                $post_data['vid'] = I('vid')?I('vid'):0;
				$post_data['pid'] = I('pid')?I('pid'):0;
                $post_data['city'] = I('city');
                $post_data['openid'] = I('openid');
				
				$this->writelog_name(var_export($post_data,TRUE),"3"); 
				//$this->writelog_name(var_export($_REQUEST,TRUE),"log_sdsd");	
             //   print_r($post_data);exit();
			     $postArray=$this->request_post($post_data);
			     // print_r($postArray);
				 // echo $postArray['result'];
				//  exit;
				  if($postArray){
				  if($postArray['result']==1){ echo"<script>alert('".$postArray['msg']."');history.go(-1);</script>"; exit; }
				  
				  
				   echo"<script>alert('恭喜您，注册成功，立即下载APP')</script>"; redirect('http://a.app.qq.com/o/simple.jsp?pkgname=com.sageit.judaren'); exit;  
				   
				 // $muser_id=(int)$postArray['msg'];
				 // $duobao_url='http://www.judaren.com.cn/jdrwechat/index.php?m=cn&c=duobao&info&pid=1&mid='.$this->encode($muser_id);
			     /*	  echo"<script>alert('恭喜您，注册成功，赠送夺宝机会一次！')</script>"; redirect($duobao_url); exit;  */
				  }
				  

				
		    /*    if($postArray){   echo"<script>alert('恭喜您，注册成功，立即下载APP')</script>"; redirect('http://a.app.qq.com/o/simple.jsp?pkgname=com.sageit.judaren'); exit;     }else{     echo"<script>alert('注册失败，请反馈管理员！');history.go(-1);</script>";  exit;  }*/

         	     exit;
        
        }


	 
            public function request_post($post_data = '') {
                $o = "";
                foreach ( $post_data as $k => $v ) 
                { 
                    $o.= "$k=" . urlencode( $v ). "&" ;
                }
                /*验证是否注册成功 返回状态说明  0  成功   result 1 失败  
                * liuq
                */
				
				 // $this->writelog_name($o,"log_sdsd2");
                $post_data = substr($o,0,-1);      
                        $get_user_info = 'http://www.judaren.com.cn/jdrapi/Api.php/Suclogin/reg_wx';
                        $uo = curl_init();
                        curl_setopt($uo,CURLOPT_URL,$get_user_info);
                        curl_setopt($uo,CURLOPT_HEADER,0); 
                        curl_setopt($uo, CURLOPT_RETURNTRANSFER, 1 );  
                        curl_setopt($uo,CURLOPT_POST,1);
                        curl_setopt($uo, CURLOPT_POSTFIELDS, $post_data);  
                        $resuo = curl_exec($uo); 
                        curl_close($uo); 
                        // $this->writelog_name($resuo,"log_sdsd3");
					    // echo   $resuo;
						//return true;
						return json_decode($resuo,true);
						
                     //   if($resuo){
						// echo 1; 
                           // redirect('http://a.app.qq.com/o/simple.jsp?pkgname=com.sageit.judaren');  
                      //  }

            }
			
			public	function encode($string = '', $skey = 'jdrwxpass') {
			$strArr = str_split(base64_encode($string));
			$strCount = count($strArr);
			foreach (str_split($skey) as $key => $value)
			$key < $strCount && $strArr[$key].=$value;
			return str_replace(array('=', '+', '/'), array('O0O0O', 'o000o', 'oo00o'), join('', $strArr));
			}

}